package sort;

public class ShellSort implements Sort{

    @Override
    public void sort(int[] arr, boolean asc) {
        int len = arr.length;
        for (int gap = len / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < len; ++i) {
                int idx = i, tmp = arr[i];
                for (; idx >= gap; idx -= gap) {
                    if ((asc && tmp >= arr[idx - gap]) || (!asc && tmp <= arr[idx - gap])) {
                        break;
                    }
                    arr[idx] = arr[idx - gap];
                }
                arr[idx] = tmp;
            }
        }
    }
}
